package com.eduhzy.ttw.teacher.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.decoration.RecycleViewDivider;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerContactComponent;
import com.eduhzy.ttw.teacher.mvp.contract.ContactContract;
import com.eduhzy.ttw.teacher.mvp.model.entity.ChildAreaData;
import com.eduhzy.ttw.teacher.mvp.presenter.ContactPresenter;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.mcxtzhang.indexlib.IndexBar.widget.IndexBar;
import com.mcxtzhang.indexlib.suspension.SuspensionDecoration;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-7-20 14:48:38
 */
@Route(path = RouterHub.APP_CONTACTFRAGMENT)
public class ContactFragment extends BaseFragment<ContactPresenter> implements ContactContract.View {

    private static final String INDEX_STRING_TOP = "↑";
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.indexBar)
    IndexBar indexBar;
    @BindView(R.id.tvSideBarHint)
    TextView tvSideBarHint;
    @Autowired(name = Constants.ROUTER_STRING)
    String areaCode = "0";
    @Autowired(name = Constants.ROUTER_INTEGER)
    int type = 0;

    @Inject
    BaseQuickAdapter<ChildAreaData, AutoBaseViewHolder> mAdapter;
    @Inject
    RecyclerView.LayoutManager layoutManager;
    @Inject
    SuspensionDecoration suspensionDecoration;
    private QMUITipDialog tipDialog;

    public static ContactFragment newInstance() {
        ContactFragment fragment = new ContactFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerContactComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_contact, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        rvList.setLayoutManager(layoutManager);
        rvList.setAdapter(mAdapter);
        rvList.addItemDecoration(suspensionDecoration);
        rvList.addItemDecoration(new RecycleViewDivider(
                getActivity(), LinearLayoutManager.VERTICAL, 1, getResources().getColor(R.color.public_color_EFEFEF)));
        indexBar.setmPressedShowTextView(tvSideBarHint)//设置HintTextView
                .setNeedRealIndex(true)//设置需要真实的索引
                .setmLayoutManager((LinearLayoutManager) layoutManager);
        mPresenter.getChildArea(areaCode);
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {
        if (tipDialog == null) {
            tipDialog = RxUtil.showLoading(getActivity(), "加载中···");
        }
        tipDialog.show();
    }

    @Override
    public void hideLoading() {
        if (ObjectUtils.isNotEmpty(tipDialog)) {
            tipDialog.dismiss();
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @Override
    public void update(List<ChildAreaData> list) {
        List<ChildAreaData> mDatas = new ArrayList<>();
        mDatas.clear();
        getActivity().getWindow().getDecorView().postDelayed(() -> {
            mDatas.add((ChildAreaData) new ChildAreaData("新朋友").setShowCount(true).setTop(true).setBaseIndexTag(INDEX_STRING_TOP));
            mDatas.add((ChildAreaData) new ChildAreaData("群组").setTop(true).setBaseIndexTag(INDEX_STRING_TOP));
            mDatas.add((ChildAreaData) new ChildAreaData("我的班级群").setTop(true).setBaseIndexTag(INDEX_STRING_TOP));
            mDatas.add((ChildAreaData) new ChildAreaData("客服").setTop(true).setBaseIndexTag(INDEX_STRING_TOP));
            mDatas.addAll(list);
            mAdapter.setNewData(mDatas);
            mAdapter.notifyDataSetChanged();
            indexBar.setmSourceDatas(mDatas).invalidate();
            suspensionDecoration.setmDatas(mDatas);
        }, 500);
    }
}
